﻿using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnitOfWorkCore.Domain.Abstractions;
using UnitOfWorkCore.Infrastructure;
using UnitOfWorkCore.Infrastructure.Context;
using UnitOfWorkCore.Infrastructure.Behaviors;
using Microsoft.Extensions.Logging;
using UnitOfWorkCore.IntegrationEvents;

namespace UnitOfWorkCore
{
    public class Startup : IStartup
    {
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddUseContext();

            //注入Uow依赖 限定了DbContext和UnitOfWork的生命周期为Scoped，整个调用链上使用的数据库上下文对象，使用是同一个类型实例
            services.AddScoped<IUnitOfWork, UnitOfWork<AppDbContext>>();

            //注入泛型仓储
            services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
            services.AddTransient(typeof(IRepository<,>), typeof(Repository<,>));

            services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Program>());
            services.AddTransient(typeof(IPipelineBehavior<,>), typeof(AppContextTransactionBehavior<,>));
            services.AddScoped<SubscriberService>();

            services.AddLogging(builder =>
               {
                   builder
                       .AddConsole() // 添加控制台输出
                       .AddDebug()   // 添加调试输出
                       .SetMinimumLevel(LogLevel.Debug); // 设置全局日志级别
               });


            return services.BuildServiceProvider();
        }

        public void Configure(IApplicationBuilder app)
        {

        }
    }
}
